// 字体图标 url - 使用更稳定的CDN源
const cssCdnUrlList = [
	'//at.alicdn.com/t/c/font_2298093_rnp72ifj3ba.css', 
	'//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css'
];
// 备用CDN源
const backupCdnUrlList = [
	'//cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css',
	'//unpkg.com/font-awesome@4.7.0/css/font-awesome.min.css'
];
// 第三方 js url
const jsCdnUrlList = [];

// 检查字体是否加载成功的函数
const checkFontLoaded = (fontFamily) => {
	return new Promise((resolve) => {
		if (document.fonts && document.fonts.check) {
			// 现代浏览器使用 Font Loading API
			resolve(document.fonts.check(`12px ${fontFamily}`));
		} else {
			// 降级方案：检查元素样式
			const testElement = document.createElement('span');
			testElement.style.fontFamily = fontFamily;
			testElement.style.fontSize = '12px';
			testElement.style.visibility = 'hidden';
			testElement.textContent = 'a';
			document.body.appendChild(testElement);
			
			const originalWidth = testElement.offsetWidth;
			testElement.style.fontFamily = 'monospace';
			const fallbackWidth = testElement.offsetWidth;
			
			document.body.removeChild(testElement);
			resolve(originalWidth !== fallbackWidth);
		}
	});
};

// 动态批量设置字体图标
export function setCssCdn() {
	if (cssCdnUrlList.length <= 0) return false;
	
	cssCdnUrlList.map((v, index) => {
		let link = document.createElement('link');
		link.rel = 'stylesheet';
		link.href = v;
		link.crossOrigin = 'anonymous';
		
		// 添加超时控制
		const timeout = setTimeout(() => {
			console.warn(`字体图标加载超时: ${v}`);
			loadBackupFont(index);
		}, 10000); // 10秒超时
		
		// 添加错误处理
		link.onerror = () => {
			clearTimeout(timeout);
			console.warn(`字体图标加载失败: ${v}`);
			loadBackupFont(index);
		};
		
		link.onload = () => {
			clearTimeout(timeout);
			showFontLoadSuccess();
		};
		
		document.getElementsByTagName('head')[0].appendChild(link);
	});
}

// 加载备用字体的函数
const loadBackupFont = (index) => {
	if (backupCdnUrlList[index]) {
		let backupLink = document.createElement('link');
		backupLink.rel = 'stylesheet';
		backupLink.href = backupCdnUrlList[index];
		backupLink.crossOrigin = 'anonymous';
		
		const backupTimeout = setTimeout(() => {
			console.error(`备用字体加载超时: ${backupCdnUrlList[index]}`);
			showFontLoadError();
		}, 8000); // 8秒超时
		
		backupLink.onerror = () => {
			clearTimeout(backupTimeout);
			console.error(`备用字体图标也加载失败: ${backupCdnUrlList[index]}`);
			showFontLoadError();
		};
		
		backupLink.onload = () => {
			clearTimeout(backupTimeout);
			showFontLoadSuccess();
		};
		
		document.getElementsByTagName('head')[0].appendChild(backupLink);
	} else {
		showFontLoadError();
	}
};

// 显示字体加载错误提示
const showFontLoadError = () => {
	// 可以在这里添加用户友好的错误提示
	console.warn('字体图标加载失败，将使用系统默认字体');
	// 如果需要，可以触发一个全局事件通知其他组件
	if (window.dispatchEvent) {
		window.dispatchEvent(new CustomEvent('fontLoadError', {
			detail: { message: '字体图标加载失败，将使用系统默认字体' }
		}));
	}
};

// 显示字体加载成功提示
const showFontLoadSuccess = () => {
	if (window.dispatchEvent) {
		window.dispatchEvent(new CustomEvent('fontLoadSuccess', {
			detail: { message: '字体图标加载成功' }
		}));
	}
};

// 动态批量设置第三方js
export function setJsCdn() {
	if (jsCdnUrlList.length <= 0) return false;
	jsCdnUrlList.map((v) => {
		let link = document.createElement('script');
		link.src = v;
		document.body.appendChild(link);
	});
}

/**
 * 批量设置字体图标、动态js
 * @method cssCdn 动态批量设置字体图标
 * @method jsCdn 动态批量设置第三方js
 */
const setIntroduction = {
	// 设置css
	cssCdn: () => {
		setCssCdn();
	},
	// 设置js
	jsCdn: () => {
		setJsCdn();
	},
};

// 导出函数方法
export default setIntroduction;
